/**
 * 项目对应的service
 */
define(function (require) {
    'use strict';
    var angular = require('angular');
    var util = require('util');
    var ui = require('ui');

    angular.module('uploadServiceApp',[])
        .factory('uploadService',['$q',function($q){
            return {
                beforeUpload : function($dialog,$dialogScope,scope){
                    $dialogScope.upload = function(){
                        if($dialogScope.uploadStatusNotify && $dialogScope.uploadStatusNotify.status === 'added'){
                            $dialogScope.uploadModel.upload();
                        }else{
                            util.alert('请选择要导入的Excel文件！');
                        }
                    };
                    $dialogScope.$watch('uploadStatusNotify', function (newValue, oldValue) {
                        if(newValue !== oldValue){
                            if(newValue.status === 'added'){
                                $dialogScope.filenameWarp = newValue.filename;
                            }else if(newValue.status === 'uploading'){
                                ui.showSucTip(newValue.msg);
                            }else if(newValue.status === 'success'){
                                ui.showSucTip(newValue.msg);
                                $dialogScope.filenameWarp = '';
                                $dialog.close();
                                scope.getList();
                            }else if(newValue.status === 'error'){
                                //ui.showErrTip(newValue.msg);
                                $dialogScope.filenameWarp = '';
                                if(newValue.retCode==='-1241000'){
                                    var url ='/api/index.php?r=ydxs/sale-report/download-excel-error&proj_id='+scope.curProjectId+'&token='+util.getToken();
                                    util.alert('<a href="'+url+'">'+newValue.msg+'</a>');
                                }else{
                                    ui.showErrTip(newValue.msg);
                                }
                            }
                        }

                    });
                },
                uploadShow : function($dialog){
                    $($dialog.node).find('[i-id=cacel]').hide();
                }
            };
        }]);
});